<template>
  <div class="emp-manager-container">
    <!-- 搜索和按钮行 -->
    <!-- 搜索区域 -->
    <el-form inline :model="queryForm" style="text-align: left; position: relative;">
      <el-form-item label="姓名">
        <el-input v-model="queryForm.ename" placeholder="请输入员工姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getTableData()">查询</el-button>
      </el-form-item>
      <el-form-item style="float: right;">
        <!-- 按钮区域 -->
        <el-button-group>
          <el-button type="primary" @click="openDialog">新增</el-button>
          <el-button type="primary" @click="getOne">编辑</el-button>
          <el-button type="primary" @click="delEmp">删除</el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
      

    <!-- 员工表格 -->
    <el-table
      ref="multipleTable"
      :data="empList"
      tooltip-effect="dark"
      style="width: 100%"
      height="430px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="工号" width="120">
        <template slot-scope="scope">{{ scope.row.empno }}</template>
      </el-table-column>
      <el-table-column label="姓名" width="100">
        <template slot-scope="scope">{{ scope.row.ename }}</template>
      </el-table-column>
      <el-table-column label="性别" width="100">
        <template slot-scope="scope">{{ scope.row.sex_name }}</template>
      </el-table-column>
      <el-table-column label="毕业学校">
        <template slot-scope="scope">{{ scope.row.college }}</template>
      </el-table-column>
      <el-table-column label="出生日期">
        <template slot-scope="scope">{{ scope.row.birthday }}</template>
      </el-table-column>
      <el-table-column label="入职日期">
        <template slot-scope="scope">{{ scope.row.hiredate }}</template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      style="margin-top: 20px;text-align: right;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[8, 10, 15, 20]"
      layout="prev, pager, next, total, sizes, jumper"
      :page-size="queryForm.limit"
      :total="total"
    ></el-pagination>

    <!-- 子组件引用 -->
    <emp-info
      :isShow="visible"
      :deptData="deptList"
      :empInfo="empForm"
      @closeDialog="setIsShow"
      @empSave="tableLoad"
    ></emp-info>
  </div>
</template>

<script>
import EmpInfo from '@/components/EmpInfo.vue'

export default {
  name: 'EmpManager',
  components: { EmpInfo },
  data() {
    return {
      empList: [],
      checkedEmpList: [],
      queryForm: {
        ename: '',
        page: 1,
        limit: 8
      },
      total: 0,
      visible: false,
      empForm: {}
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.checkedEmpList = val
    },
    getTableData() {
      this.$http.post('/getEmps', this.queryForm).then(res => {
        this.empList = res.data.data
        this.total = res.data.count
      })
    },
    openDialog() {
      this.visible = true
    },
    setIsShow() {
      this.visible = false
    },
    getOne() {
      if (this.checkedEmpList.length === 0) {
        this.$message.warning('请选择要修改的信息')
        return
      }
      if (this.checkedEmpList.length > 1) {
        this.$message.warning('只能选择一条数据')
        return
      }
      let empno = this.checkedEmpList[0].empno
      this.$http.get('/getEmp/' + empno).then(res => {
      this.empForm = res.data
      this.visible = true
})
    },
    tableLoad(msg) {
      if (msg === 'success') {
        this.visible = false
        this.$message.success('员工信息保存成功')
        this.getTableData()
      }
    },
    handleSizeChange(val) {
      this.queryForm.limit = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.getTableData()
    },
    delEmp() {
      if (this.checkedEmpList.length === 0) {
        this.$message.warning('请选择要删除的数据')
        return
      }
      this.$confirm('确定要删除选中数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let empnos = []
        for (let emp of this.checkedEmpList) {
          empnos.push(emp.empno)
        }
        this.$http.post('/delBatchEmps', empnos).then(res => {
          if (res.data === 'success') {
            this.$message.success('删除成功')
            this.getTableData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  created() {
    this.getTableData()
    this.getDeptList()
  }
}
</script>

